
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hpssim;

import java.util.ArrayList;
import java.util.*;

/**
 *
 * @author Igor Neri <igor@cimice.net>
 */
public class Queue {
    
        public ArrayList <Job> q;
        private int currentMaxPriority;
        private int currentPriority;
        private int size;
        private int strategy;
        public Queue(int _strategy){
            strategy = _strategy;
            q = new ArrayList<Job>();
            this.size = 0;
            this.currentMaxPriority = -10000;
        }
        /*
         * Add methods: add, remove, search element for features
         */
        
        public void push(Job j){
            if (q.isEmpty()) {
                q.add(j);
                this.size++;
                this.currentMaxPriority = j.ps;
            }
            else{
                switch (strategy){
                    case 0: /*FIFO*/
                        q.add(j);
                        this.size++;
                        currentMaxPriority = j.ps;
                        break;
                    case 1: /*PR*/
                        currentPriority = j.ps;                        
                        if (currentPriority > currentMaxPriority){
                            q.add(0,j);
                            currentMaxPriority = currentPriority;
                            this.size++;
                        }
                        else if (currentPriority <= q.get(q.size()-1).ps){
                                q.add(j);                                
                                this.size++;
                        }else{
                                for (int i = 0; i < q.size(); i++) {
                                    if (currentPriority > q.get(i).ps) {
                                        q.add(i,j);
                                        this.size++;
                                        break;
                                    }
                                }
                        }
                        
                }
            }                        
            //this.printpriority();
        }
        public Job pop() {            
            if (q.isEmpty()) {
                System.out.println("ERROR QUEUE is Empty non si fanno dei pop inutili");
                return null;
            } else {                                               
                this.size--;                       
                return q.remove(0);
                
            }
        }
        public Job getFirstCPUJob(){
            if (q.isEmpty()){
                return null;
            }
            else{
                for (int i = 0; i < q.size(); i++){
                    if (q.get(i).classification == 0 ){
                        this.size--;
                        return q.remove(i);
                        
                    }
                }
                return null;
            }           
        }
        public Job getFirstGPUJob(){
            if (q.isEmpty()){
                return null;
            }
            else{
                for (int i = 0; i < q.size(); i++){
                    if (q.get(i).classification == 1 ){
                        this.size--;
                        return q.remove(i);
                        
                    }
                }
                return null;
            }           
        }
        public void sort(){
            Collections.sort(q, new Comparator(){
                public int compare(Object o1, Object o2) {
                    Job j1 = (Job) o1;
                    Job j2 = (Job) o2;
                    if (j1.ps > j2.ps) return 1;
                    else    return -1;
                }
            });
            
            
        }
        public int size(){
            return this.size;
        }
        public void printpriority(){
            for (int i = 0; i < q.size(); i++){
                System.out.print(q.get(i).getps());
            }
            
        }
        public void printjob(){
            for (int i = 0; i < q.size(); i++){
                System.out.print(q.get(i).getid()+ " ");
            }
            
        }
        public void printJobsStat(){
            for (int i = 0; i < q.size(); i++){
                q.get(i).printjobstat();
                //System.out.println();
            }
            
        }
        
        public Job getJob(int index){
            this.size--;
            return q.remove(index);
        }
        
        public int getIndexFirstGPUjob(){
            int index = -1;
            for (int i = 0; i < q.size(); i++){
                if (q.get(i).getclass() == 1){
                    index = i;
                    break;
                }
            }
            return index;
        }
        public int getIndexFirstCPUjob(){
            int index = -1;
            for (int i = 0; i < q.size(); i++){
                if (q.get(i).getclass() == 0){
                    index = i;
                    break;
                }
            }
            return index;
        }
        public void newpriority(){
            int oldprio = 0;
            int newprio = 0;
            for (int i=0; i < q.size(); i++){
                oldprio = q.get(i).getpp();
                newprio = oldprio + q.get(i).getps();
                q.get(i).setps(newprio);
                oldprio = 0;
                newprio = 0;
            }
            this.sort();
        }
}
